package com.hit.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hit.eduService.entity.EduSubject;
import com.hit.eduService.entity.excel.SubjectData;
import com.hit.eduService.entity.subject.OneSubject;
import com.hit.eduService.entity.subject.TwoSubject;
import com.hit.eduService.listener.SubjectExcelListener;
import com.hit.eduService.mapper.EduSubjectMapper;
import com.hit.eduService.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-03
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try{
            //文件输入流
            InputStream in = file.getInputStream();
            //读取excel文件
            //由于EduSubjectServiceImpl对象需要new 监听器，所以把它传入SubjectExcelListener的构造方法中
            //直接传this对不对？  可能直接用this是这个类本身的对象而不是spring管理的对象
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查数据库构造List<OneSubject>用来返回
        List<OneSubject> ans=new ArrayList<>();

        //1.查询所有一级分类 parent_id==0
        QueryWrapper<EduSubject> wrapper1=new QueryWrapper<>();
        wrapper1.eq("parent_id","0");
        //baseMapper对象在EduSubjectServiceImpl继承的ServiceImpl中自动装配
        List<EduSubject> oneSubjectList=baseMapper.selectList(wrapper1);

        //2.查询所有2级分类 parent_id!=0
        QueryWrapper<EduSubject> wrapper2=new QueryWrapper<>();
        wrapper2.ne("parent_id","0");
        //baseMapper对象在EduSubjectServiceImpl继承的ServiceImpl中自动装配
        List<EduSubject> twoSubjectList=baseMapper.selectList(wrapper2);

        //3把一级分类加入到ans中
        for(int i=0;i<oneSubjectList.size();i++){
            EduSubject eduSubject=oneSubjectList.get(i);
            OneSubject oneSubject=new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);
            ans.add(oneSubject);
        }
        //4 把二级分类添加到ans中
        for(int i=0;i<twoSubjectList.size();i++){
            EduSubject eduSubject=twoSubjectList.get(i);
            //把这个二级分类对应的一级分类找到

            for(int j=0;j<ans.size();j++){//枚举所有一级分类
                if(eduSubject.getParentId().equals(ans.get(j).getId())){
                    //如果eduSubject这一项的parent_id找到对应的一级分类，就把它转换成TwoSubject对象存到一级分类的字段中
                    TwoSubject twoSubject=new TwoSubject();
                    BeanUtils.copyProperties(eduSubject,twoSubject);
                    ans.get(j).getChildren().add(twoSubject);
                }
            }
        }
        return ans;
    }
}
